//
//  KWLScanCodeViewController.m
//  KWNLaywerClient
//
//  Created by 孔六五 on 14/12/10.
//  Copyright (c) 2014年 张明磊. All rights reserved.
//

#import "KWLScanCodeViewController.h"
#import "ZBarSDK.h"
#import <AVFoundation/AVFoundation.h>
#import "KFLIsqrcodeRequest.h"
#import "KWLScanCodeLoginViewController.h"
#import "KWLScanCodeWebViewController.h"
#import "KWLLoginViewController.h"
#import "RESideMenu.h"

@interface KWLScanCodeViewController ()<AVCaptureMetadataOutputObjectsDelegate,UIImagePickerControllerDelegate,ZBarReaderDelegate,ZBarReaderViewDelegate>
@property (weak, nonatomic) UIView *imageView_title;
@property NSInteger num;
@property BOOL upOrdown;
@property BOOL scanfirst;

@property (strong,nonatomic)AVCaptureDevice * device;
@property (strong,nonatomic)AVCaptureDeviceInput * input;
@property (strong,nonatomic)AVCaptureMetadataOutput * output;
@property (strong,nonatomic)AVCaptureSession * session;
@property (strong,nonatomic)AVCaptureVideoPreviewLayer * preview;
@property (nonatomic, retain) UIImageView * line;

@property (nonatomic,strong) NSString *code;
@property (nonatomic,strong) ZBarReaderView *readerView;

@end

@implementation KWLScanCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];
    _scanfirst = YES;
    UIView *imageView_title = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44 + IOS7_SIZE)];
    imageView_title.backgroundColor = RGB(91, 165, 229);
    [self.view addSubview:imageView_title];
    _imageView_title = imageView_title;
    
    UIButton *button_menu = [UIButton buttonWithType:UIButtonTypeCustom];
    button_menu.frame = CGRectMake(0, IOS7_SIZE, 60, 45);
    [button_menu setImage:[UIImage imageFileName:@"btn_leftList.png"] forState:UIControlStateNormal];
    //button_menu.tag = KWNDetailViewButtonTag_back;
    [button_menu addTarget:self action:@selector(didClickButton_scancodeBack:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button_menu];
    
    UILabel *label_title = [[UILabel alloc] initWithFrame:CGRectMake(imageView_title.frame.origin.x,IOS7_SIZE + 5,imageView_title.frame.size.width,35)];
    label_title.backgroundColor = [UIColor clearColor];
    label_title.textColor = ColorOfTitle;
    label_title.textAlignment = NSTextAlignmentCenter;
    label_title.font = FontOfHelveticaBold(16);
    [self.view addSubview:label_title];
    label_title.text = @"扫一扫";

    if (IOS7_VERSION) {
        [self ios7ScanCode];
        [self setupCamera];
        
    }
    else
    {
        [self ios6ScanCode];
    }
    

}

- (void)dealloc
{
    if (_timer) {
        [_timer invalidate];
    }
    _imageView_title = nil;
    if (_readerView) {
        _readerView = nil;
    }
    if (_line) {
        _line = nil;
    }
    if (_device) {
        _device = nil;
    }
    if (_input) {
        _input = nil;
    }
    if (_output) {
        _output = nil;
    }
    if (_session) {
        _session = nil;
    }
    if (_preview) {
        _preview = nil;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    if (_scanfirst == YES) {
        _scanfirst = NO;
    }
    else
    {
        [self codeStartAgain];
    }

}

- (void)setupCamera
{
//    self.view.backgroundColor = RGBAlpha(0, 0, 0, 0.2);
    // Device
    _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    // Input
    _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
    
    // Output
    _output = [[AVCaptureMetadataOutput alloc]init];
    [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    // Session
    _session = [[AVCaptureSession alloc]init];
//    [_session setSessionPreset:AVCaptureSessionPresetHigh];
    
    if ([_session canAddInput:self.input])
    {
        [_session addInput:self.input];
    }
    
    if ([_session canAddOutput:self.output])
    {
        [_session addOutput:self.output];
    }
    
    // 条码类型 AVMetadataObjectTypeQRCode
    _output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode];
//    _output.rectOfInterest = CGRectMake(60,110,280,280);
    // Preview
    _preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session];
    _preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
//    _preview.frame =CGRectMake(60,110,280,280);
    [_preview setFrame:CGRectMake(0, _imageView_title.frame.origin.y + _imageView_title.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - _imageView_title.frame.origin.y + _imageView_title.frame.size.height)];
    [self.view.layer insertSublayer:self.preview atIndex:0];
    // Start
    [_session startRunning];
}

- (void)didClickButton_scancodeBack:(id)sender
{
    [[RESideMenu sharedInstance] presentMenuViewController];
    [MobClick event:@"caidan"];
    /*
    [self dismissViewControllerAnimated:YES completion:^{
        [_timer invalidate];
    }];
     */
}

#pragma mark - 判断扫到的二维码并决定触发的事情 -

- (void)getCodeTodo
{
    if (self.code)
    {
        NSString *httpStr = @"http";
        NSString *kflStr = @"{\"content\"";
        NSRange httpRange=[self.code rangeOfString:httpStr];
        NSRange kflRange=[self.code rangeOfString:kflStr];

        
        //{"content":"1","code":"1418363321780712"}
        if(httpRange.length>0 &&httpRange.location  == 0)
        {
            [self codeToHttpView];
        }
        else if(kflRange.length>0 &&kflRange.location  == 0)
        {
            [self codeToKWL];
        }
        else
        {
            [self codeStartAgain];
        }
    }
    else
    {
        [self codeStartAgain];
    }
}

#pragma mark - 扫码重新启动 -
- (void)codeStartAgain
{
    if (IOS6_VERSION) {
        _num = 0;
        _upOrdown = NO;
        _timer = nil;
        [_timer invalidate];
        [NSTimer initialize];
        //定时器，设定时间过1.5秒，
        _timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(ios6Animation1) userInfo:nil repeats:YES];
        [_readerView start];
    }
    else
    {
        _upOrdown = NO;
        _num =0;
        _timer = nil;
        [_timer invalidate];
        [NSTimer initialize];

        _timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(ios7Animation1) userInfo:nil repeats:YES];
        
        [_session startRunning];
    }

}

//扫码http跳转
- (void)codeToHttpView
{
    KWLScanCodeWebViewController *scanCodeWeb = [[KWLScanCodeWebViewController alloc] initWithUrlStr:self.code];
    [self pushViewController:scanCodeWeb];
}

- (void)codeToKWL
{
    NSDictionary *dic = [self dicFromCode];
    if (dic) {
        NSString *content = [dic objectForKey:@"content"];
        if (content) {
            if([content isEqualToString:@"1"])
            {
                [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
                
                __weak KWLScanCodeViewController *aself = self;
                KFLIsqrcodeRequest *isqrcode = [[KFLIsqrcodeRequest alloc] initRequest];
                isqrcode.stu = @"-1";
                isqrcode.code = [dic objectForKey:@"code"];
                [isqrcode startToRequestWithBlock:^(NSInteger stateCode, NSMutableArray *result) {
                    switch (stateCode) {
                        case 10000:
                        {
                            [SVProgressHUD dismiss];
                            KWLScanCodeLoginViewController *weblogin = [[KWLScanCodeLoginViewController alloc] initWithCodeStr:self.code];
                            [aself pushViewController:weblogin];
                        }
                            break;
                        case 10003:
                        {
                            [SVProgressHUD dismiss];
                            [aself codeToWebLoginTokenIsFailure];

                        }
                            break;
                        default:
                        {
                            NSString *string_msg  = result[0];
                            [SVProgressHUD dismissWithError:string_msg afterDelay:1.5];
                            double delayInSeconds = 1.5;
                            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
                            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                                [aself codeStartAgain];
                            });
    
                        }
                            break;
                    }
                }];
                
            }
            
        }
    }
}

- (NSDictionary *)dicFromCode
{
    NSData* data_content = [self.code dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;
    NSDictionary *codeDic = [NSJSONSerialization JSONObjectWithData:data_content options:NSJSONReadingMutableLeaves error:&error];
    if (!error) {
        return codeDic;
    }
    return nil;
}


#pragma mark - ios7使用系统自带的-

- (void)ios7ScanCode
{
    
//    UILabel * labIntroudction= [[UILabel alloc] initWithFrame:CGRectMake(15, 40, 290, 50)];
//    labIntroudction.backgroundColor = [UIColor clearColor];
//    labIntroudction.numberOfLines=2;
//    labIntroudction.textColor=[UIColor whiteColor];
//    labIntroudction.text=@"将二维码图像置于矩形方框内，离手机摄像头10CM左右，系统会自动识别。";
//    [self.view addSubview:labIntroudction];
    
    UIImageView * image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"codeCover.png"]];
    image.frame = CGRectMake(0, _imageView_title.frame.origin.y + _imageView_title.frame.size.height + Iphone5_size(0, -20), self.view.frame.size.width , self.view.frame.size.height - _imageView_title.frame.origin.y + _imageView_title.frame.size.height);
    image.contentMode = UIViewContentModeTop;
    [self.view insertSubview:image atIndex:0];
    _upOrdown = NO;
    _num =0;
    
    _line = [[UIImageView alloc] initWithFrame:CGRectMake(60, 115 , 200, 8)];
    _line.image = [UIImage imageNamed:@"line.png"];
    [image addSubview:_line];
    
    UIView *view_kuang = [[UIView alloc] initWithFrame:CGRectMake(60, 115, 200, 210)];
    view_kuang.backgroundColor = [UIColor clearColor];
    view_kuang.layer.borderColor = RGB(62, 120, 181).CGColor;
    view_kuang.layer.borderWidth = 1;
    [image addSubview:view_kuang];
    
    NSString *deviceStr = [UIUtils currentDevicePlatformString];
    if ([deviceStr isEqualToString:@"iPhone 4"] || [deviceStr isEqualToString:@"iPhone 4S"]) {
        
        UIImageView *imageview_toptitle = [[UIImageView alloc] initWithFrame:CGRectMake((image.frame.size.width - 155)/2, 40, 155, 46)];
        imageview_toptitle.image = [UIImage imageFileName:@"scancode_toptitle.png"];
        [image addSubview:imageview_toptitle];
        
        UIImageView *imageview_codeInstructions = [[UIImageView alloc] initWithFrame:CGRectMake((image.frame.size.width - 165)/2, 115 + 205 + Iphone5_size(50, 30), 165, 12)];
        imageview_codeInstructions.image = [UIImage imageFileName:@"scancode_bottomtitle.png"];
        [image addSubview:imageview_codeInstructions];
    }
    else
    {
        UIImageView * imageview_codeInstructions = [[UIImageView alloc] initWithFrame:CGRectMake((image.frame.size.width - 210)/2, 115 + 205 + Iphone5_size(25, 15), 210, 140)];
        imageview_codeInstructions.image = [UIImage imageFileName:@"scancode_Iphone5.png"];
        [image addSubview:imageview_codeInstructions];

    }

    _timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(ios7Animation1) userInfo:nil repeats:YES];

    
}

#pragma mark - ios7上下扫描动画 -

- (void)ios7Animation1
{
    if (_upOrdown == NO) {
        _num ++;
        _line.frame = CGRectMake(60, 110+2*_num, 200, 8);
        if (2*_num == 204) {
            _upOrdown = YES;
        }
    }
    else {
        _num --;
        _line.frame = CGRectMake(60, 110+2*_num, 200, 8);
        if (_num == 0) {
            _upOrdown = NO;
        }
    }

}


#pragma mark - ios6使用ZBar -

//- (void)ios6ScanCode
//{
//    _num = 0;
//    _upOrdown = NO;
//    
//    //初始话ZBar
//    ZBarReaderViewController *reader = [ZBarReaderViewController new];
//    //设置代理
//    reader.readerDelegate = self;
//    //支持界面旋转
//    reader.supportedOrientationsMask = ZBarOrientationMaskAll;
//    reader.showsHelpOnFail = NO;
//    reader.scanCrop = CGRectMake(0.1, 0.2, 0.8, 0.8);//扫描的感应框
//    ZBarImageScanner * scanner = reader.scanner;
//    [scanner setSymbology:ZBAR_I25
//                   config:ZBAR_CFG_ENABLE
//                       to:0];
//    
//    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, _imageView_title.frame.origin.y + _imageView_title.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - _imageView_title.frame.origin.y - _imageView_title.frame.size.height)];
//    view.backgroundColor = [UIColor clearColor];
//    reader.cameraOverlayView = view;
//    
//    
//    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 40)];
//    label.text = @"请将扫描的二维码至于下面的框内\n谢谢！";
//    label.textColor = [UIColor whiteColor];
//    label.textAlignment = 1;
//    label.lineBreakMode = 0;
//    label.numberOfLines = 2;
//    label.backgroundColor = [UIColor clearColor];
//    [view addSubview:label];
//    
//    UIImageView * image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pick_bg.png"]];
//    image.frame = CGRectMake(20, 80, 280, 280);
//    [view addSubview:image];
//    
//    _line = [[UIImageView alloc] initWithFrame:CGRectMake(30, 10, 220, 2)];
//    _line.image = [UIImage imageNamed:@"line.png"];
//    [image addSubview:_line];
//    //定时器，设定时间过1.5秒，
//    _timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(ios6Animation1) userInfo:nil repeats:YES];
//    
//    [self presentViewController:reader animated:YES completion:^{
//        
//    }];
//
//}

- (void)ios6ScanCode
{
    _num = 0;
    _upOrdown = NO;
    
    _readerView = [[ZBarReaderView alloc]init];
    _readerView.frame = CGRectMake(0, _imageView_title.frame.origin.y + _imageView_title.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - _imageView_title.frame.origin.y - _imageView_title.frame.size.height);
    _readerView.readerDelegate = self;
    //关闭闪光灯
    _readerView.torchMode = 0;
    //扫描区域
//    CGRect scanMaskRect = CGRectMake(40, CGRectGetMidY(readerView.frame) - 80, 280, 280);
//    readerView.scanCrop = [self getScanCrop:scanMaskRect readerViewBounds:readerView.bounds];
    _readerView.scanCrop = CGRectMake(0.15, 0.1, 0.8, 0.8);
    _readerView.tracksSymbols = NO;
    _readerView.trackingColor = [UIColor clearColor];
    
    //处理模拟器
    if (TARGET_IPHONE_SIMULATOR) {
        ZBarCameraSimulator *cameraSimulator = [[ZBarCameraSimulator alloc]initWithViewController:self];
        cameraSimulator.readerView = _readerView;
    }
    
    [self.view addSubview:_readerView];
    //扫描区域计算
    
// CS 快法务 KLW 2014-12-15
//    UIImageView * image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pick_bg.png"]];
//    image.frame = CGRectMake((_readerView.frame.size.width - 280)/2, 80, 280, 280);
//    [_readerView addSubview:image];
//    
    UIImageView * image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"codeCover.png"]];
    image.frame = CGRectMake(0, Iphone5_size(0, -20), _readerView.frame.size.width,_readerView.frame.size.height);
    image.contentMode = UIViewContentModeTop;
    [_readerView addSubview:image];

// CE 快法务 KLW 2014-12-15
    
// CS 快法务 KLW 2014-12-15
//    _line = [[UIImageView alloc] initWithFrame:CGRectMake((image.frame.size.width - 220)/2, 10, 220, 2)];
//    
    _line = [[UIImageView alloc] initWithFrame:CGRectMake(60, 115 , 200, 8)];

// CE 快法务 KLW 2014-12-15
    _line.image = [UIImage imageNamed:@"line.png"];
    [image addSubview:_line];
    
    UIView *view_kuang = [[UIView alloc] initWithFrame:CGRectMake(60, 115, 200, 210)];
    view_kuang.backgroundColor = [UIColor clearColor];
    view_kuang.layer.borderColor = RGB(62, 120, 181).CGColor;
    view_kuang.layer.borderWidth = 1;
    [image addSubview:view_kuang];

    NSString *deviceStr = [UIUtils currentDevicePlatformString];
    if ([deviceStr isEqualToString:@"iPhone 4"] || [deviceStr isEqualToString:@"iPhone 4S"] || [deviceStr isEqualToString:@"iPod Touch 4G"]) {
        UIImageView *imageview_toptitle = [[UIImageView alloc] initWithFrame:CGRectMake((image.frame.size.width - 155)/2, 40, 155, 46)];
        imageview_toptitle.image = [UIImage imageFileName:@"scancode_toptitle.png"];
        [image addSubview:imageview_toptitle];
        
        UIImageView *imageview_codeInstructions = [[UIImageView alloc] initWithFrame:CGRectMake((image.frame.size.width - 165)/2, 115 + 205 + Iphone5_size(50, 30), 165, 12)];
        imageview_codeInstructions.image = [UIImage imageFileName:@"scancode_bottomtitle.png"];
        [image addSubview:imageview_codeInstructions];
    }
    else
    {
        UIImageView * imageview_codeInstructions = [[UIImageView alloc] initWithFrame:CGRectMake((image.frame.size.width - 210)/2, 115 + 205 + Iphone5_size(20, 10), 210, 140)];
        imageview_codeInstructions.image = [UIImage imageFileName:@"scancode_Iphone5.png"];
        [_readerView addSubview:imageview_codeInstructions];
        
    }

//    UIImageView * imageview_codeInstructions = [[UIImageView alloc] initWithFrame:CGRectMake((image.frame.size.width - 143)/2, 115 + 205 + Iphone5_size(50, 30), 143, 36)];
//    imageview_codeInstructions.image = [UIImage imageFileName:@"codeInstructions.png"];

    //定时器，设定时间过1.5秒，
    _timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(ios6Animation1) userInfo:nil repeats:YES];

    [_readerView start];

}

// DS 快法务 KLW 2014-12-12
//-(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)readerViewBounds
//{
//    CGFloat x,y,width,height;
//    
//    x = rect.origin.x / readerViewBounds.size.width;
//    y = rect.origin.y / readerViewBounds.size.height;
//    width = rect.size.width / readerViewBounds.size.width;
//    height = rect.size.height / readerViewBounds.size.height;
//    
//    return CGRectMake(x, y, width, height);
//}
//
// DE 快法务 KLW 2014-12-12
#pragma mark - ios6扫描结果
- (void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image
{
    [_timer invalidate];
    _line.frame = CGRectMake(_line.frame.origin.x, 10, 200, 8);
    _num = 0;
    _upOrdown = NO;

    NSString *result = nil;
    
    for (ZBarSymbol *symbol in symbols) {
        NSLog(@"%@", symbol.data);
        
        
        
        if ([symbol.data canBeConvertedToEncoding:NSShiftJISStringEncoding])
            
        {
            result = [NSString stringWithCString:[symbol.data cStringUsingEncoding: NSShiftJISStringEncoding] encoding:NSUTF8StringEncoding];
        }
        else
        {
            result = symbol.data;
        }
        
        self.code = result;
        break;
    }
    
    [readerView stop];
    [self getCodeTodo];
}

#pragma mark - ios6上下扫描动画 -

-(void)ios6Animation1
{
    if (_upOrdown == NO) {
        _num ++;
        _line.frame = CGRectMake(_line.frame.origin.x, 115 + 2*_num, 200, 8);
        if (2*_num == 200) {
            _upOrdown = YES;
        }
    }
    else {
        _num --;
        _line.frame = CGRectMake(_line.frame.origin.x, 115 + 2*_num, 200, 8);
        if (_num == 0) {
            _upOrdown = NO;
        }
    }
    
    
}


// DS 快法务 KLW 2014-12-12
//#pragma mark - UIImagePickerControllerDelegate -
//#pragma mark - ios6
//-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
//{
//    [_timer invalidate];
//    _line.frame = CGRectMake(30, 10, 220, 2);
//    _num = 0;
//    _upOrdown = NO;
//    [picker dismissViewControllerAnimated:YES completion:^{
//        [picker removeFromParentViewController];
//    }];
//}
//
//
//-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
//{
//    [_timer invalidate];
//    _line.frame = CGRectMake(30, 10, 220, 2);
//    _num = 0;
//    _upOrdown = NO;
//    [picker dismissViewControllerAnimated:YES completion:^{
//        [picker removeFromParentViewController];
//        UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
//        //初始化
//        ZBarReaderController * read = [ZBarReaderController new];
//        //设置代理
//        read.readerDelegate = self;
//        CGImageRef cgImageRef = image.CGImage;
//        ZBarSymbol * symbol = nil;
//        id <NSFastEnumeration> results = [read scanImage:cgImageRef];
//        for (symbol in results)
//        {
//            break;
//        }
//        NSString * result;
//        if ([symbol.data canBeConvertedToEncoding:NSShiftJISStringEncoding])
//            
//        {
//            result = [NSString stringWithCString:[symbol.data cStringUsingEncoding: NSShiftJISStringEncoding] encoding:NSUTF8StringEncoding];
//        }
//        else
//        {
//            result = symbol.data;
//        }
//        
//        
//        NSLog(@"%@",result);
//    }];
//}
//
// DE 快法务 KLW 2014-12-12
#pragma mark AVCaptureMetadataOutputObjectsDelegate
#pragma mark - ios7
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    _num = 0;
    _upOrdown = NO;

    
    [_session stopRunning];
    [_timer invalidate];

    NSString *stringValue;
    
    if ([metadataObjects count] >0)
    {
        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
        stringValue = metadataObject.stringValue;
    }
    

    self.code = stringValue;
    NSLog(@"%@",stringValue);
    [self getCodeTodo];

//    [self dismissViewControllerAnimated:YES completion:^
//     {
//         [_timer invalidate];
//         NSLog(@"%@",stringValue);
//     }];
}

#pragma mark - 账号被替换下线提示 -
- (void)codeToWebLoginTokenIsFailure{
    UIAlertView *alter_login;
    if(IOS8_VERSION){
        alter_login = [[UIAlertView alloc] initWithTitle:@"您的来问律师账号已在其它地方登录，请您重新登录此设备。如果不是您的操作，您的密码可能已泄露，请及时更改密码，或联系快问律师客服邮箱laiwenlvshi@kuaifawu.com。" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    }
    else{
        alter_login = [[UIAlertView alloc] initWithTitle:nil message:@"您的来问律师账号已在其它地方登录，请您重新登录此设备。如果不是您的操作，您的密码可能已泄露，请及时更改密码，或联系快问律师客服邮箱laiwenlvshi@kuaifawu.com。" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    }
    alter_login.tag = 10003;
    [alter_login show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (alertView.tag) {
        case 10003:{
            KWLLoginViewController *login = [[KWLLoginViewController alloc] initWithTag:6];
            [self pushViewController:login];
        }break;
            
        default:
            break;
    }
}

@end
